import { lk_buyer,lk_common,lk_expert,lk_seller,lk_manager,lk_consumer } from '@/config/myConfig.js';
import storage from "@/utils/storage.js"
// 对网络请求重新封装，等待登陆后请求，防止token失败
function request(method, url, data, header) {
	return new Promise((resolve, reject) => {
		if (getApp().globalData._IsLogin_) {
			// console.warn("直接登陆："+url);
			requestX(method, url, data, header).then(function(data) {
				resolve(data);
			}).catch(function(data) {
				reject(data);
			})
		} else {
			// 设置定时器，等待登陆完成
			let cnt = 0;
			let tid = setInterval(() => {
				if (cnt > 2000) { //设置最大等待时间
					clearInterval(tid);
					reject("请求超时，请刷新重试！");
					return;
				}
				// console.warn("等待登陆完成："+(++cnt));
				if (getApp().globalData._IsLogin_) {
					// console.warn("等待登陆成功："+cnt+" ---"+url);
					clearInterval(tid);
					requestX(method, url, data, header).then(function(data) {
						resolve(data);
					}).catch(function(data) {
						reject(data);
					})
				}
			}, 10)
		}
	})
}

function requestX(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': uni.getStorageSync('token'),
		'Authorization_jbs': uni.getStorageSync('token')
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				//  uni.hideLoading() 
				if (res && res.code.code === 200) {
					try {
						const _data_ = JSON.parse(res.data || '{}');
						resolve(_data_);
					} catch (error) {
						resolve(res.data);
					}
				} else {
					uni.showToast({
						title: res.message || '服务器响应超时,请稍后再试！',
						icon: 'none'
					});
					reject(res.message);
				}
			},
			fail: function(res) {
				console.log(res, 9999)
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res.data);
			}
		});
	})
}

function requesthttpLogin(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
	}, header)
	return new Promise((resolve, reject) => {
		uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				uni.hideLoading();
				resolve(res);
			},
			fail: function(res) {
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requesthttp(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'accessToken': storage.getAccessToken(),
		'refreshToken': storage.getRefreshToken(),
		// 'Authorization': uni.getStorageSync('token'),
		// 'Authorization_jbs': uni.getStorageSync('token'),
		// 'accessToken':null,
	}, header)
	return new Promise((resolve, reject) => {
		uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				uni.hideLoading();
				resolve(res);
			},
			fail: function(res) {
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requestlist(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization':storage.getAccessToken(),
		'Authorization_jbs': storage.getAccessToken()
	}, header)
	return new Promise((resolve, reject) => {
		if (getApp().globalData._IsLogin_) {
			// console.warn("直接登陆："+url);
			requestlistX(method, url, data, header).then(function(data) {
				resolve(data);
			}).catch(function(data) {
				reject(data);
			})
		} else {
			// 设置定时器，等待登陆完成
			let cnt = 0;
			let tid = setInterval(() => {
				if (cnt > 500) { //设置最大等待时间
					clearInterval(tid);
					reject("请求超时，请刷新重试！");
					return;
				}
				// console.warn("等待登陆完成："+(++cnt));
				if (getApp().globalData._IsLogin_) {
					// console.warn("等待登陆成功："+cnt+" ---"+url);
					clearInterval(tid);
					requestlistX(method, url, data, header).then(function(data) {
						resolve(data);
					}).catch(function(data) {
						reject(data);
					})
				}
			}, 10)
		}
	})
}

function requestlistX(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': storage.getAccessToken(),
		'Authorization_jbs': storage.getAccessToken()
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success(res) {
				// uni.hideLoading();
				resolve(res.data);
			},
			fail: function(res) {
				// uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requestRight(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': storage.getAccessToken(),
		'Authorization_jbs': storage.getAccessToken(),
		'accessToken': storage.getAccessToken(),
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url: url,
			method,
			header: headers,
			success(res) {
				// uni.hideLoading();
				resolve(res.data);
			},
			fail: function(res) {
				// uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}
export const settingApi = {
	// 设置
	/**
	 * @param { expertId }
	 * @description 根据专家ID查询专家坐班时间
	 * @detail 
	 */
	s_allTimePeriods: (expertId,data) => requesthttp('GET', `${lk_expert}/expert/site/siteRoomTime/allTimePeriodsByExpertId/${expertId}`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}), 
	
	/**
	 * @param { id }
	 * @description 根据ID获取专家信息-服务设置价格
	 * @detail 已联调
	 */
	s_expertPrice: (id,data) => requesthttp('GET', `${lk_expert}/expert/expert/expert/${id}`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	/**
	 * @param {  }
	 * @description 专家服务设置发起审核
	 * @detail 已联调
	 */
	initiateAudit: (data) => requesthttp('POST', `${lk_expert}/expert/expert/expert/service/initiateAudit`, data, {
		'content-type': 'application/json'
	}),
	/**
	 * @param { ... }
	 * @description 带条件分页查询数据-选择院区和场地
	 * @detail 已联调
	 */
	sitePagelist: (data) => requesthttp('POST', `${lk_expert}/expert/site/site/pagelist`, data, {
		'content-type': 'application/json'
	}),
	/**
	 * @param { siteId }
	 * @description 查询场地的诊室列表-选择院区和场地
	 * @detail 已联调
	 */
	siteRoom: (siteId,data) => requesthttp('POST', `${lk_expert}/expert/site/site/getSiteRoom/${siteId}`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),	
	/**
	 * @param { siteRoomId }
	 * @description 根据诊室ID查询诊室所有时间段
	 * @detail 已联调
	 */
	allTimePeriodsBySiteRoomId: (siteRoomId,data) => requesthttp('GET', `${lk_expert}/expert/site/siteRoomTime/allTimePeriodsBySiteRoomId/${siteRoomId}`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),	
	// 服务开关
	/**
	 * @param { expertId }
	 * @description 专家查看在线咨询，线下会诊状态
	 * @detail 已联调
	 */
	registerOpenConsultOpen: (data) => requesthttp('POST', `${lk_expert}/expert/expert/expert/getRegisterOpenConsultOpen`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),	
	/**
	 * @param {  }
	 * @description 专家开启或关闭在线咨询
	 * @detail 已联调
	 */
	consultOpen: (data) => requesthttp('POST', `${lk_expert}/expert/expert/expert/consultOpen`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),	
	/**
	 * @param {  }
	 * @description 专家开启或关闭线下会诊
	 * @detail 已联调
	 */
	registerOpen: (data) => requesthttp('POST', `${lk_expert}/expert/expert/expert/registerOpen`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),	
	/**
	 * @param {  }
	 * @description 专家开启或关闭线下会诊
	 * @detail 已联调
	 */
	forceCloseRegister: (data) => requesthttp('POST', `${lk_expert}/expert/expert/expert/forceCloseRegister`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	
	
	/**
	 * @param {  }
	 * @description 验证专家旧手机号码
	 * @detail 已联调
	 */
	verificationOldPhone: (data) => requesthttp('POST', `${lk_expert}/expert/passport/verificationOldPhone`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),  
	/**
	 * @param {  }
	 * @description 专家-修改新的手机号码
	 * @detail 已联调
	 */
	editPhone: (data) => requesthttp('POST', `${lk_expert}/expert/passport/editPhone`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	/**
	 * @param {  }
	 * @description 修改密码
	 * @detail 已联调
	 */
	modifyPass: (data) => requesthttp('PUT', `${lk_expert}/expert/passport/modifyPass`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
}
